<?php
  // 新增用户后端保存程序
  include_once('../conn.php');
  $username = $_POST['username'];
  $password1 = $_POST['password1'];
  $password2 = $_POST['password2'];
  $real_name = $_POST['real_name'];

  $sql = "select * from user where username = '$username'";
  $result = mysqli_query($conn, $sql);
  if($result) {
    alert('该用户名已存在', 'user_new.php');
    exit();
  }
  if(strlen($username) < 5) {
    alert('用户名不能小于5位', 'user_new.php');
    exit();
  }
  if($password1 != $password2) {
    alert('两次输入密码不相同，请重新输入', 'user_new.php');
    exit();
  }

  $sql = "insert into user (username, password, real_name) values ('$username', '$password1', '$real_name')";
  $result = mysqli_query($conn, $sql);
  if($result) {
    $_SESSION['username'] = $username;
    alert('添加成功', 'user_list.php');
  } else {
    alert('添加失败', 'user_new.php');
  }

  mysqli_close($conn);
?>